<template>
  <div class="playMusic">
    <div
      class="bg"
      :style="{ backgroundImage: `url(${playDetail.al.picUrl})` }"
    ></div>
    <div class="playTop">
      <div class="back" @click="$emit('back')">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-zuojiantou"></use>
        </svg>
      </div>
      <div class="center">
        <div class="title">
          <marquee behavior="scroll" direction="left">
            {{ playDetail.al.name }}
          </marquee>
        </div>
      </div>
      <div class="share">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-fenxiang"></use>
        </svg>
      </div>
    </div>
    <div class="playContent" v-if="!isLyric" @click="isLyric = true">
      <img
        class="needle"
        :class="{ active: !playFlag }"
        src="@/assets/img/needle-ab.png"
        alt=""
      />
      <img class="disc" src="@/assets/img/bofang.png" alt="" />
      <img class="playing" :src="playDetail.al.picUrl" alt="" />
    </div>

    <div
      class="playLyric"
      v-if="isLyric"
      ref="playLyric"
      @touchmove="touchmoveChange"
      @click="isLyric = false"
    >
      <p
        :class="{
          activeScroll:
            currentTime * 1000 >= parseInt(item.time) &&
            currentTime * 1000 < parseInt(item.pre),
        }"
        v-for="(item, i) in lyricList"
        :key="i"
      >
        {{ item.lyric }}
      </p>
    </div>
    <div class="progress"></div>
    <div class="playFooter">
      <svg class="icon" aria-hidden="true" @click="goPlay(0)">
        <use xlink:href="#icon-danxunhuan"></use>
      </svg>
      <svg class="icon" aria-hidden="true" @click="goPlay(-1)">
        <use xlink:href="#icon-shangyishoushangyige"></use>
      </svg>
      <svg v-if="playFlag" class="icon play" aria-hidden="true" @click="play">
        <use xlink:href="#icon-bofang1-copy"></use>
      </svg>
      <svg v-if="!playFlag" class="icon play" aria-hidden="true" @click="play">
        <use xlink:href="#icon-bofang-copy"></use>
      </svg>
      <svg class="icon" aria-hidden="true" @click="goPlay(1)">
        <use xlink:href="#icon-xiayigexiayishou"></use>
      </svg>
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-liebiao"></use>
      </svg>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions, mapMutations } from "vuex";
import emitter from "@/util/bus.js";
export default {
  props: ["playDetail", "playFlag", "play"],
  data() {
    return {
      isLyric: false,
      flaghuadong: true,
    };
  },
  computed: {
    ...mapState(["lyric", "currentTime", "playlist", "playCurrentIndex"]),
    ...mapGetters(["lyricList"]),
  },
  watch: {
    currentTime(newval, oldval) {
      if(this.isLyric){
        if (this.flaghuadong) {
          let p = document.querySelector(".activeScroll");
          console.log(this.$refs, 111111);
          if(p&&p.offsetTop&& p.offsetHeight){
            this.$refs.playLyric.scrollTop = p.offsetTop - p.offsetHeight * 3;
          }
        }
      }
    },
  },
  methods: {
    ...mapActions(["reqLyric"]),
    ...mapMutations(["setchangeSongs"]),
    //手指触发时，防止继续滚动
    touchmoveChange(e) {
      this.flaghuadong = false;
      let p = setTimeout(() => {
        this.flaghuadong = true;
        // 清除定时器
        clearTimeout(p);
      }, 3000);
    },
    // 上一首和下一首
    goPlay(num) {
      console.log(3333,num)
      this.setchangeSongs(num);
      console.log(this.currentTime,4444)
      this.reqLyric(this.playlist[this.playCurrentIndex]);
      setTimeout(() => {
        emitter.emit("updatePlay", {
          playFlag: false,
        });
      }, 200);
    },
  },
};
</script>

<style lang="less" scoped>
.playMusic {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  z-index: 9999999;
  .bg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    background-size: auto 100%;
    background-position: center;
    filter: blur(20px);
  }
  .playTop {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 7.5rem;
    height: 1.2rem;
    position: absolute;
    left: 0;
    top: 0;
    color: #fff;
    padding: 0 0.4rem;
    .icon {
      fill: #fff;
      width: 0.4rem;
      height: 0.4rem;
    }
    marquee {
      width: 5rem;
      flex: 1;
    }
  }
  .playContent {
    position: absolute;
    width: 7.5rem;
    height: 7.5rem;
    left: 0;
    top: 1.5rem;
    .needle {
      width: 2.3rem;
      height: auto;
      position: absolute;
      left: 3.5rem;
      z-index: 100;
      transform-origin: 0.3rem 0;
      transform: rotate(-15deg);
      transition: all 1s;
    }
    .needle.active {
      width: 2.3rem;
      height: auto;
      position: absolute;
      left: 3.5rem;
      z-index: 100;
      transform-origin: 0.3rem 0;
      transform: rotate(30deg);
      transition: all 1s;
    }
    .disc {
      width: 5.5rem;
      height: auto;
      position: absolute;
      left: calc(50% - 2.75rem);
      z-index: 10;
      top: 2.5rem;
    }
    .playing {
      width: 4rem;
      height: 4rem;
      border-radius: 1.5rem;
      position: absolute;
      left: calc(50% - 2rem);
      top: 3rem;
    }
  }
  .playFooter {
    width: 7.5rem;
    height: 1.5rem;
    position: absolute;
    left: 0;
    bottom: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 0.4rem;
    .icon {
      fill: #fff;
      width: 0.4rem;
      height: 0.4rem;
    }
    .play.icon {
      width: 1rem;
      height: 1rem;
    }
  }
  .playLyric {
    position: absolute;
    width: 7.5rem;
    height: 8rem;
    left: 0;
    top: calc(50% - 4rem);
    overflow-y: scroll;
    text-align: center;
    color: #999;
    padding: 0.2rem 0;
    line-height: 1rem;
    .activeScroll {
      color: red;
    }
  }
}
</style>
